package com.yunxi.service.localService.impl.sto;

import com.yunxi.core.util.WgrDate;
import com.yunxi.model.order.PmsVehicleMaintainRecord;
import com.yunxi.sto.pojo.VehicleMaintainItemVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sto.VehicleMaintainItemMapper;
import com.yunxi.model.sto.VehicleMaintainItem;
import com.yunxi.service.localService.sto.VehicleMaintainItemService;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 车辆库存保养项目 服务实现类
 * </p>
 *
 * @author Guan
 * @since 2019-10-14
 */
@Service
public class VehicleMaintainItemServiceImpl extends ServiceImplPlus<VehicleMaintainItemMapper, VehicleMaintainItem> implements VehicleMaintainItemService {

    @Override
    public void saveBatchByMaintainId(PmsVehicleMaintainRecord record, List<VehicleMaintainItemVO> listVehicleMaintainItem) {
        //删除之前所有记录
        baseMapper.deleteByMaintainId(record.getMaintainId());
        //重新添加
        Date currentDate = new Date();
        if(listVehicleMaintainItem!=null){
            for (int i=0;i<listVehicleMaintainItem.size();i++){
                VehicleMaintainItemVO vo = listVehicleMaintainItem.get(i);
                VehicleMaintainItem vmi = new VehicleMaintainItem();
                vo.setMaintainRecordId(record.getMaintainId());
                if(StringUtils.isEmpty(vo.getMaintainPeriod())){
                    vo.setMaintainPeriod(record.getMaintainPeriod());
                }

                if(StringUtils.isEmpty(vo.getVin())){
                    vo.setVin(record.getVin());
                }

                vo.setMaintainDate(currentDate);

                BeanUtils.copyProperties(vo,vmi);
                this.saveOrUpdate(vmi);
            }
        }
    }
}
